package com.stock.demo.function.mine.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.stock.demo.R;
import com.stock.demo.framework.ui.fragment.BaseFragment;
import com.stock.demo.function.mine.presenter.MinePresenter;
import com.stock.demo.function.mine.presenter.MinePresenterImpl;
import com.stock.demo.function.start.view.LoginActivity;
import com.stock.demo.util.SettingUtils;
import com.suke.widget.SwitchButton;

/**
 * Created by XuZhaoyu on 2017/6/1.
 */

public class MineFragment extends BaseFragment implements MineView {

    private View mView;

    private MinePresenter mPresenter;
    private SwitchButton mSbAutoLogin;
    private TextView mTvLogout;
    private SwitchButton mSbForceRefresh;
    private LinearLayout mLlRefreshSpeed;

    @Override
    public View createFragmentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_mine, container, false);
        return mView;
    }

    @Override
    public void initView() {
        mSbAutoLogin = (SwitchButton) mView.findViewById(R.id.sb_mine_auto_login);
        mSbForceRefresh = (SwitchButton) mView.findViewById(R.id.sb_mine_force_refresh);
        mLlRefreshSpeed = (LinearLayout) mView.findViewById(R.id.ll_mine_refresh_speed);
        mTvLogout = (TextView) mView.findViewById(R.id.tv_mine_logout);
        mSbAutoLogin.setChecked(SettingUtils.checkAutoLogin());
        mSbForceRefresh.setChecked(SettingUtils.checkForceRefresh());
    }

    @Override
    public void initData() {
        mPresenter = new MinePresenterImpl(this);
        mSbAutoLogin.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                mPresenter.setAutoLogin(isChecked);
            }
        });
        mSbForceRefresh.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                mPresenter.setForceRefresh(isChecked);
            }
        });
        mLlRefreshSpeed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SpeedChooseDialog(getContext()).show();
            }
        });
        mTvLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.logout();
            }
        });
    }

    @Override
    public void onLogout() {
        LoginActivity.startAction(getContext());
        getActivity().finish();
    }

}
